<!--
  * 巡检异常处理表
  *
-->
<template>

  <a-layout style="height: 100%">
    <a-layout-sider style="background-color: white ">

      <a-tree
          style="margin-top: 20px"
          :tree-data="treeData"
          :default-expanded-keys="['0-0-0', '0-0-1']"
          @select="onSelect"
          v-if="treeData.length > 0"
          default-expand-all="true"

      >
        <!--        @select="onSelect"
                @check="onCheck"-->
        <!--
                <span slot="title0010" style="color: #1890ff">sss</span>
        -->
      </a-tree>
    </a-layout-sider>
    <a-layout style="">

      <a-layout-content>
    <!---------- 查询表单form begin ----------->
    <a-form class="smart-query-form">
        <a-row class="smart-query-form-row">
            <a-form-item label="异常描述" class="smart-query-form-item">
                <a-input style="width: 150px" v-model:value="queryForm.description" placeholder="异常描述" />
            </a-form-item>
            <a-form-item class="smart-query-form-item">
                <a-button type="primary" @click="queryDatanew">
                    <template #icon>
                        <SearchOutlined />
                    </template>
                    搜索
                </a-button>

          <a-button @click="resetQuery" class="smart-margin-left10">
                    <template #icon>
                        <ReloadOutlined />
                    </template>
                    重置
                </a-button>
              <a-button @click="exportData('tableExcel','result')" class="smart-margin-left10">
                <template #icon>
                  <ReloadOutlined />
                </template>
                导出
              </a-button>
            </a-form-item>
        </a-row>
    </a-form>
    <!---------- 查询表单form end ----------->

    <a-card size="small" :bordered="false" :hoverable="true">
        <!---------- 表格操作行 begin ----------->
        <a-row class="smart-table-btn-block">
            <div class="smart-table-operate-block">
<!--                <a-button @click="showForm" type="primary" size="small">
                    <template #icon>
                        <PlusOutlined />
                    </template>
                    新建
                </a-button>-->
            </div>
            <div class="smart-table-setting-block">
                <TableOperator v-model="columns" :tableId="null" :refresh="queryData" />
            </div>
        </a-row>
        <!---------- 表格操作行 end ----------->

        <!---------- 表格 begin ----------->
        <a-table
            id="tableExcel"
                size="small"
                :dataSource="tableData"
                :columns="columns"
                rowKey="id"
                bordered
                :loading="tableLoading"
                :pagination="false"
        >
            <template #bodyCell="{ text, record, column }">
                <template v-if="column.dataIndex === 'action'">
                    <div class="smart-table-operate">
                      <a-tag  color="red" @click="showForm(record)">查看</a-tag>
                      <a-tag v-if="record.state === '1'" color="green" @click="showForm(record)">指派</a-tag>
                    </div>
                </template>

              <template v-if="column.dataIndex === 'state'">
                <div class="smart-table-operate">
                  <!--               {{record.status}}-->
                  <div v-if="record.state === '1'" color="red">待指派</div>
                  <div v-if="record.state === '2'" color="red">待处理</div>
                  <div v-if="record.state === '3'" color="red">已处理</div>
                </div>
              </template>
            </template>

<!--          //1待指派2待处理3已处理-->

        </a-table>
        <!---------- 表格 end ----------->

        <div class="smart-query-table-page">
            <a-pagination
                    showSizeChanger
                    showQuickJumper
                    show-less-items
                    :pageSizeOptions="PAGE_SIZE_OPTIONS"
                    :defaultPageSize="queryForm.pageSize"
                    v-model:current="queryForm.pageNum"
                    v-model:pageSize="queryForm.pageSize"
                    :total="total"
                    @change="queryData"
                    @showSizeChange="queryData"
                    :show-total="(total) => `共${total}条`"
            />
        </div>

        <GInspectionAbnormalForm  ref="formRef" @reloadList="queryData"/>

    </a-card>
  </a-layout-content>

  </a-layout>
  </a-layout>
</template>
<script setup>
    import { reactive, ref, onMounted } from 'vue';
    import { message, Modal } from 'ant-design-vue';
    import { SmartLoading } from '/@/components/framework/smart-loading';
    import { gInspectionAbnormalApi } from '/@/api/business/xjgl/g-inspection-abnormal-api';
    import { PAGE_SIZE, PAGE_SIZE_OPTIONS } from '/@/constants/common-const';
    import { smartSentry } from '/@/lib/smart-sentry';
    import TableOperator from '/@/components/support/table-operator/index.vue';
    import GInspectionAbnormalForm from './g-inspection-abnormal-form.vue';
    import {departmentApi} from "/@/api/system/department-api.js";
    // ---------------------------- 表格列 ----------------------------
    const treeData = ref([]);
    onMounted(queryGuanQuTree);

    async function queryGuanQuTree() {
      let res = await departmentApi.queryGuanQuTree();
      treeData.value= res.data;
    }

    const columns = ref([
      /*  {
            title: '巡检任务ID',
            dataIndex: 'taskId',
            ellipsis: true,
        },*/
        {
            title: '巡检线路',
            dataIndex: 'lineDetail',
            ellipsis: true,
        },
        {
            title: '组织机构',
            dataIndex: 'deptName',
            ellipsis: true,
        },
        {
            title: '异常描述',
            dataIndex: 'description',
            ellipsis: true,
        },
        {
            title: '异常处理状态1待指派2待处理3已处理',
            dataIndex: 'state',
            ellipsis: true,
        },
        {
            title: '上报人',
            dataIndex: 'createUserName',
            ellipsis: true,
        },
        {
            title: '上报时间',
            dataIndex: 'createTime',
            ellipsis: true,
        },
        {
            title: '整改建议',
            dataIndex: 'handlingOpinions',
            ellipsis: true,
        },
        {
            title: '操作',
            dataIndex: 'action',
            fixed: 'right',
            width: 130,
        },
    ]);

    // ---------------------------- 查询数据表单和方法 ----------------------------
    const selectedKeys = ref([]);

    function onSelect(keys, event) {
      console.log('Trigger Select', keys, event);


      const key  = event.node.dataRef.key;
      console.log(key)
      console.log("keys[0]"+keys[0]);

      if(keys[0]!=null){
        queryForm.deptId=keys[0];
        queryData();
      }


    }
    const queryFormState = {
        description: undefined, //异常描述
        pageNum: 1,
        pageSize: PAGE_SIZE,
    };
    // 查询表单form
    const queryForm = reactive({ ...queryFormState });
    // 表格加载loading
    const tableLoading = ref(false);
    // 表格数据
    const tableData = ref([]);
    // 总数
    const total = ref(0);

    // 重置查询条件
    function resetQuery() {
        let pageSize = queryForm.pageSize;
        Object.assign(queryForm, queryFormState);
        queryForm.pageSize = pageSize;
        queryData();
    }
    async function queryDatanew() {
      queryForm.pageNum=1;
      queryData();
    }

    import * as XLSX from 'xlsx/xlsx.mjs';
    import FileSaver from "file-saver";
    function exportData(tablename, filename) {

      var wb = XLSX.utils.book_new()
      var xlsxData = XLSX.utils.table_to_sheet(document.querySelector("#" + tablename))


      XLSX.utils.book_append_sheet(wb, xlsxData, filename)
      var wbout = XLSX.write(wb, {
        bookType: 'xlsx',
        bookSST: true,
        type: 'array'
      })

      try {
        FileSaver.saveAs(
            new Blob([wbout], {type: "application/octet-stream"}),
            filename + '.xlsx'
        )
      } catch (e) {
        if (typeof console !== "undefined")
          console.log(e, wbout)
      }

    }
    // 查询数据
    async function queryData() {
        tableLoading.value = true;
      if(queryForm.deptId==0){
        queryForm.deptId="";
      }
        try {
            let queryResult = await gInspectionAbnormalApi.queryPage(queryForm);
            tableData.value = queryResult.data.list;
            total.value = queryResult.data.total;
        } catch (e) {
            smartSentry.captureError(e);
        } finally {
            tableLoading.value = false;
        }
    }


    onMounted(queryData);

    // ---------------------------- 添加/修改 ----------------------------
    const formRef = ref();

    function showForm(data) {
        formRef.value.show(data);
    }

</script>
